<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,minimum-scale=1.0,maximum-scale=1.0,initial-scale=1.0" />
    <title>沃闪购</title>
    <link rel="stylesheet" href="../css/normalize.css" />
    <link rel="icon" href="../img/bitbug_favicon.ico" type="image/x-icon" />
    <!--<link rel="stylesheet" href="index.css"/>-->
    <!--<link rel="stylesheet" href="../index.css"/>-->
    <link rel="stylesheet" href="../index_format.css" />
    <link rel="stylesheet" href="../css/layer.css" />

    <script type="text/javascript" src="../js/jquery.min.js"></script>
    <script type="text/javascript" src="../js/rem.js"></script>
    <script type="text/javascript" src="../js/tool.js"></script>
    <script>
        setTimeout(function() {
            document.documentElement.style.height = window.innerHeight + 'px';
        }, 1000)
    </script>
</head>

<body onload="preventMove()">
    <div class="header">
        <div><img src="../img/wo-logo.png" alt="" /></div>
        <div class="wo-topic">沃闪购合作营销平台</div>
    </div>
    <div class="container">
        <div class="flex-box">
            <div class="choose-reset">
                <form id="resetForm" action="">
                    <ul class="form-list">
                        <li><label for="wo_phone">手机号：</label><input id="wo_phone" name="userName" type="text" autofocus value="" placeholder="请输入11位手机号" /></li>
                        <li><label for="wo_pwd">新密码：</label><input id="wo_pwd" name="password" type="password" value="" placeholder="请输入6位以上密码" /></li>
                        <li><label for="wo_pwd2">新密码确认：</label><input id="wo_pwd2" name="password2" type="password" value="" placeholder="请输入6位相同密码" /></li>
                        <li>
                            <input id="get_code" type="button" value="发送验证码" /><input id="wo_code" name="code" type="text" name="putcode" value="" placeholder="请输入验证码" />
                        </li>

                    </ul>
                </form>
                <div class="margin-center"></div>
                <div class="btn-group">
                    <a href="javascript:layer_cofirm('确认取消修改？');" class="btn-style">返回</a>
                    <a id="resetBtn" href="#" class="btn-style">确认修改</a>
                </div>
            </div>
        </div>
    </div>
    <script type="text/javascript" src="../js/layer.js"></script>

    <script>
        function layer_msg(mgs) {
            layer.open({
                content: mgs,
                skin: 'msg',
                time: 2
            })
        }

        function layer_cofirm(mgs) {
            layer.open({
                content: mgs,
                btn: ['确定', '取消'],
                style: 'font-size:.32rem',
                shadeClose: false,
                yes: function(index) {
                    location.href = './login.html';
                    layer.close(index);
                }
            })
        }
        var resetPassword = {
            wo_phone: "#wo_phone",
            get_code: "#get_code",
            resetForm: "#resetForm",
            wo_code: "#we_code",
            resetBtn: '#resetBtn',
            wo_pwd: '#wo_pwd',
            wo_pwd2: '#wo_pwd2',
            host_server: getServerHost(),
            url: '/api/shopping/sendVerifyCode?',
            checkCode: '/api/shopping/checkVerifyCode?',
            changePassword: '/api/shopping/changePassword?',
            init: function() {
                this.getCode();
                this.resetPassword();
            },
            getCode: function() {
                var context = this;

                $(context.get_code).click(function(e) {
                    var wait = 60;

                    function testPhone() {
                        var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/;
                        if (!myreg.test($(context.wo_phone).val())) {
                            layer_msg('请输入有效的手机号码！');
                            return false;
                        }
                        return true;
                    }

                    function time(o) {
                        console.log(o)
                        if (wait == 0) {
                            $(o).attr('disabled', false);
                            $(o).val('获取验证码');
                            wait = 60;
                        } else {
                            $(o).attr('disabled', true);
                            $(o).val(wait + "秒后重新发送");
                            wait--;
                            setTimeout(function() {
                                time(o)
                            }, 1000)
                        }
                    }
                    if (testPhone()) {
                        if (!$(context.get_code).isDisabled) {
                            time(context.get_code);
                            $.ajax({
                                type: 'POST',
                                data: 'phone=' + $(context.wo_phone).val(),
                                url: context.host_server + context.url,
                                success: function(response) {
                                    if (200 == response.code) {
                                        layer_msg('短信已发送，注意查收');
                                    }
                                },
                                error: function() {

                                }
                            })
                            //context.checkPhone();
                            console.log('发送了请求')
                        }
                    } else {
                        layer_msg('请输入有效的手机号码！')
                    }
                });
            },
            //验证表单
            registerCheck: function() {
                var context = this;
                //验证手机
                function testPhone() {
                    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/;
                    if (!myreg.test($(context.wo_phone).val())) {
                        layer_msg('请输入有效的手机号码！');
                        return false;
                    }
                    return true;
                }

                function checkPassword(str) {
                    var reg2 = /([a-zA-Z0-9!@#$%^&*()_?<>{}]){6,18}/;
                    if (reg2.test(str)) {
                        return true;
                    } else if (!reg2.test(str)) {
                        layer_msg("密码长度在6-18位之间");
                        return false;
                    } else {
                        return true;
                    }
                }

                function testIdentify() {
                    if ($(context.wo_pwd).val() !== $(context.wo_pwd2).val()) {
                        layer_msg('两次的输入不一样');
                        return false;
                    } else if ($(context.wo_pwd).val() == "") {
                        layer_msg('请输入密码');
                        return false;
                    } else {
                        return true;
                    }
                }
                return testPhone() && checkPassword($('#wo_pwd').val()) && testIdentify();
            },
            //修改验证
            resetPassword: function() {
                var context = this;
                $(context.resetBtn).click(function(e) {
                    e.preventDefault;
                    if (context.registerCheck()) {
                        var formData = $(context.resetForm).serializeArray()
                        formData.splice(2, 1)
                        formData = arry2json(formData)
                        console.log(formData);
                        $.ajax({
                            type: 'POST',
                            // contentType: "application/json;charset=UTF-8",
                            data: 'phone=' + formData.userName + "&code=" + formData.code,
                            url: context.host_server + context.checkCode,
                            dataType: 'json',
                            success: function(response) {
                                console.log(response);
                                if (200 == response.code) {
                                    if (!response.res) {
                                        layer_msg('输入的验证码错误，请稍后再试');
                                    } else {
                                        $(context.resetForm)[0].reset();
                                        $.ajax({
                                            type: 'POST',
                                            data: 'phone=' + formData.userName + "&password=" + formData.password,
                                            url: context.host_server + context.changePassword,
                                            dataType: 'json',
                                            success: function(response) {
                                                console.log(response)
                                                if (200 == response.code) {
                                                    layer_cofirm('改密成功，跳转到主页')
                                                }
                                            },
                                            error: function() {}
                                        })
                                    }
                                }
                            },
                            error: function() {}
                        })
                        // fromData=JSON.parse(formData)
                    }
                })
            }
        }
        resetPassword.init();
    </script>
</body>

</html>
